﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;
using VideoClubGorbea.Model;
using VideoClubGorbea.Common.Interfaces;
using VideoClubGorbea.Common.Utils;
using System.Linq.Expressions;


namespace VideoClubGorbea.Infraestructure
{

    public class VideoClubGorbeaContext : DbContext, IVIdeoClubGorbeaContext
    {
        public VideoClubGorbeaContext()
            : base("VideoClubGorbea")
        {
           
            QueryableExtensions.Includer = new DbIncluder();           
        }

        private class DbIncluder : QueryableExtensions.IIncluder
        {
            public IQueryable<T> Include<T, TProperty>(IQueryable<T> source, Expression<Func<T, TProperty>> path)
                where T : class
            {
                return DbExtensions.Include(source, path);
            }
        }

        public DbSet<Movie> Movies { get; set; }
        public DbSet<Person> Persons { get; set; }
        public DbSet<User> Users { get; set; }
        public DbSet<Image> Images { get; set; }       


        public new void SaveChanges()
        {
            base.SaveChanges();
            
        }

        public void Save<T>(T entity) where T : class, IEntity
        {
            this.Set<T>().Add(entity);
        }

        public void Delete<T>(T entity) where T : class, IEntity
        {
            this.Set<T>().Remove(entity);
        }

        IQueryable<Movie> IVIdeoClubGorbeaContext.Movies
        {
            get { return this.Movies.AsQueryable(); }
        }

        IQueryable<User> IVIdeoClubGorbeaContext.Users
        {
            get { return this.Users.AsQueryable(); }
           
        }
    }
}
